package com.lw.yygh.user.controller.api;

import com.lw.yygh.model.user.UserInfo;
import com.lw.yygh.result.Result;
import com.lw.yygh.user.service.UserInfoService;
import com.lw.yygh.utils.AuthContextHolder;
import com.lw.yygh.vo.user.LoginVo;
import com.lw.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;
    //用户手机号登录接口
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
        Map<String,Object> info= userInfoService.loginUser(loginVo);
        return Result.ok( info );
    }
    //用户认证接口
    @PostMapping("/auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //传递两个参数，第一个参数用户id，认证数据的vo对象
        userInfoService.userAuth( AuthContextHolder.getUserId( request ),userAuthVo );
        return Result.ok();
    }
    //根据token获取用户id查询用户信息接口
    @GetMapping("/auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId( request );
        UserInfo userInfo = userInfoService.getById( userId );
        return Result.ok( userInfo );
    }
}
